package com.bto.solar.work.service.biz_stage_handler;

import com.bto.solar.work.common.BizException;
import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskEnum;
import com.bto.solar.work.constant.BizTaskStateEnum;
import com.bto.solar.work.constant.Constants;
import com.bto.solar.work.dao.IOrderBaseService;
import com.bto.solar.work.entity.OrderState;
import com.bto.solar.work.lease.LeaseApi;
import com.bto.solar.work.service.BtoLeaseReviewService;
import com.bto.solar.work.service.BtoOrderStateService;
import com.bto.solar.work.utils.NullUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 王小波
 * @description: 并网阶段处理器
 * @date 2023/7/24 7:59
 */

@Slf4j
@Component
public class BingWangStageHandler implements StageHandler {
    @Autowired
    private IOrderBaseService orderService;
    @Autowired
    private BtoOrderStateService btoOrderStateService;
    @Autowired
    private JieSuanStageHandler jieSuanStageHandler;
    @Autowired
    private BtoLeaseReviewService btoLeaseReviewService;
    @Autowired
    private LeaseApi leaseApi;

    @Override
    public void createNewInitState(String orderId, String stageId, String taskId) {
        btoOrderStateService.createOrderState(orderId, BizStageEnum.GRID_CONNECTION.getStageId(), BizTaskEnum.TASK_BWXX.getTaskId(), BizTaskStateEnum.WAITING_FILLED.getStateId());
    }

    /**
     * 并网信息直接退给越秀审核
     */
    @Override
    public void updateToWaitingApprovalState(String orderId, String stageId, String taskId) {
        //施工审核可能触发重签，并网必须要重签才能推送并网-不然越秀不能审核并网信息
        OrderState signedOrderState = btoOrderStateService.getOrderState(orderId, BizStageEnum.LEASE_SIGN.getStageId(),
                BizTaskEnum.TASK_HTQY.getTaskId(), BizTaskStateEnum.SIGNED.getStateId());
        if(NullUtils.isNull(signedOrderState)){
            throw new BizException("施工审核触发资方合同重签，完成重签才能推送并网");
        }
        //推送越秀-并网信息审查
        val operatingRes = leaseApi.operatingSignUpdate(orderService.getById(orderId));
        if(NullUtils.isNull(operatingRes)){
            throw new BizException("推送越秀并网信息审核，推送失败，请重新操作 订单{}" , orderId);
        }
        val pushFileed = leaseApi.isFileAndContactSyncPush(orderId, Constants.BINGWANG_FILE_LIST, true);
        if(!pushFileed){
            throw new BizException("推送资方，并网信息，推送并网信息图片失败 订单{} , 请重新操作" , orderId);
        }
        //重置越秀审核状态记录
        btoLeaseReviewService.clearLeaseReviewState(orderId, Constants.AGAIN_OPERATING_SIGN_UPDATE);

        btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.GRID_CONNECTION.getStageId(), BizTaskEnum.TASK_BWXX.getTaskId(),
                BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId(),
                BizTaskStateEnum.WAITING_FILLED.getStateId(),
                BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId());
    }

    @Transactional
    @Override
    public void updateToApprovalPassState(String orderId, String stageId, String taskId, String approvalType) {
        if (Constants.APPROVAL_LEASE.equals(approvalType)) {
            btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.GRID_CONNECTION.getStageId(), BizTaskEnum.TASK_BWXX.getTaskId(), BizTaskStateEnum.APPROVAL_PASS_LEASE.getStateId(),
                    BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId(), false);
            //开启结算
            jieSuanStageHandler.createNewInitState(orderId, BizStageEnum.SETTLEMENT.getStageId(), null);
        }
    }

    @Override
    public void updateToApprovalRejectState(String orderId, String stageId, String taskId, String approvalType , Object rejectInfo) {
        btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.GRID_CONNECTION.getStageId(), BizTaskEnum.TASK_BWXX.getTaskId(),
                BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId(),
                BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId(), true);
    }

}
